<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\goods\Goods as GoodsGoods;
use app\api\server\Goods as ServerGoods;
use app\common\model\goods\Category;
use app\common\model\goods\Review;
use app\common\model\user\Collect;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['category', 'goods_lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $params = request()->param();
        extract($params);
        $lists = Category::all();
        $this->success('', $lists);
    }


    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (商品列表)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="integer", required=true, sample="类型（不传为全部）", description="1=经典爆款,2=私人定制,3=封坛用酒")
     * @ApiParams (name="category_id", type="integer", required=true, sample="分类ID（不传为全部）", description="分类ID（不传为全部）")
     * @ApiParams (name="keywords", type="integer", required=true, sample="", description="搜索条件")
     * @ApiParams (name="home", type="integer", required=true, sample="", description="1=首页推荐,2=商品列表")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_lists()
    {
        $params = request()->param();
        try {
            $group_id = $this->auth->isLogin() ? $this->auth->group_id : 0;

            $lists = ServerGoods::goods_list($params, $group_id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (商品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $params = request()->param();
        extract($params);
        $data = GoodsGoods::get($id);
        if (!$data) {
            $this->error('商品不存在');
        }
        if ($this->auth->isLogin()) {
            $uid = $this->auth->id;
            $data->vip_price = ServerGoods::vip_price($data, $this->auth->group_id);
            $data->is_collect = Collect::get(['goods_id' => $id, 'user_id' => $uid]) ? true : false;
        } else {
            $data->is_collect =  false;
        }
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @date: 2023-12-06 
     * @ApiTitle (商品收藏)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_collect()
    {
        $params = request()->param();
        extract($params);
        $uid = $this->auth->id;
        $info = Collect::get(['goods_id' => $id, 'user_id' => $uid]);
        if (!$info) {
            Collect::create([
                'goods_id' => $id,
                'user_id' => $uid,
            ], true);
        } else {
            $info->delete();
        }
        $this->success('成功');
    }
    /**
     * @Author LK
     * @date: 2023-12-06 
     * @ApiTitle (商品评论)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_evaluate()
    {
        $params = request()->param();
        extract($params);
        $lists = Review::with(['user'])->where('goods_id', $id)->order('review.id desc')->paginate();
        $this->success('成功', $lists);
    }
}
